/*
 * CommandeAvancer.cpp
 *
 *  Created on: 19 nov. 2012
 *      Author: aboursie - potier
 */

#include "CommandeAvancer.h"

void CommandeAvancer::execute() {
	try{
		robot->avancer(x, y);
	}catch (EtatRobot::BadAction){
		pile.pop();
	}
}

bool CommandeAvancer::initialiserArguments(vector<string> args) {
	bool flag = false;
	if (args.size() == 2) {
		x = atoi(args[0].c_str());
		y = atoi(args[1].c_str());
		flag = true;
	}else{
		pile.pop();
	}
	return flag;
}

Commande* CommandeAvancer::constructeurVirtuel() {
	CommandeAvancer* tmp = new CommandeAvancer(robot);
	tmp->x = x;
	tmp->y = y;
	if (pile.size() == 0) {
		tmp->last_x = 0;
		tmp->last_y = 0;
	} else {
		tmp->last_x = x;
		tmp->last_y = y;
	}
	pile.push(tmp);
	return tmp;
}

void CommandeAvancer::desexecute() {
	if (pile.size() == 1) {
		last_x = 0;
		last_y = 0;
	}
	robot->avancer(last_x, last_y);
	x = last_x;
	y = last_y;
	pile.pop();
}
